package com.demo.thread.demo_thread_create_way;

import java.util.concurrent.*;

/**
 * @author peter
 * @date 2022/3/18 上午1:08
 */
public class Demo4 {

    private static class CallableDemo implements Callable<String> {
        @Override
        public String call() throws Exception {
            printThreadInfo();
            return "callable";
        }
    }

    private static class RunableDemo implements Runnable {
        @Override
        public void run() {
            System.out.println("runable");
            printThreadInfo();
        }
    }

    /**
     * 通过线程池方式运行
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.execute(()->{
            System.out.println("runable test");
            printThreadInfo();
        });

        // execute 不可以接收返回值
        RunableDemo runableDemo = new RunableDemo();
        executorService.execute(runableDemo);
        executorService.submit(runableDemo);

        // submit 可以接收返回值
        Future<String> result = executorService.submit(new CallableDemo());
        System.out.println(result.get());
    }

    private static void printThreadInfo() {
        System.out.println("===========");
        System.out.println("当前线程ID" + Thread.currentThread().getId());
        System.out.println("当前线程名称" + Thread.currentThread().getName());
        System.out.println("当前线程状态" + Thread.currentThread().getState());
        System.out.println("当前线程优先级" + Thread.currentThread().getPriority());
    }
}
